// pages/camel_card/my_camelCard_recharge/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    var that = this;
        wx.request({
                url: app.url + 'api_account/role_list',
                data: {
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (json) {
                  console.log(json)
                  if(json.data.done==false){
                     prompt(json.data.message, 'loading', true);
                  }else{
                    if(json.data.result.pwdStatus==0){
                      prompt("请先设置支付密码", "loading");
                     setTimeout(() => { wx.redirectTo({ url: '/pages/camel_card/my_camelCard_find_password/index', }); }, 1200);
                    }
                    that.setData({ 'lists': (json.data.result.list ? json.data.result.list : [])})
                  }
                }
            })
  },
  //用户点击充值
  recharge_click: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id,
      amount = e.currentTarget.dataset.amount,
      index = e.currentTarget.dataset.index;

    /*if (amount == 0) {
      prompt("充值失败", "loading");
      return;
    }*/
        wx.request({
                url: app.url + 'api_account/account_recharge',
                data: {
                  roleId:id
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (json) {
                  console.log(json)
                  if(json.data.done==false){
                     prompt(json.data.message, 'loading', true);
                  }else{
                        wx.requestPayment({
                      'appId': json.data.result.appId,
                      'timeStamp': json.data.result.timeStamp.toString(),
                      'nonceStr': json.data.result.nonceStr,
                      'package': json.data.result.package,
                      'signType': 'MD5',
                      'paySign': json.data.result.sign,
                      'success': function (res) {

                         prompt("充值成功", "success");
                         setTimeout(() => { 
                          wx.redirectTo({  url: '/pages/camel_card/my_camelCard/index' }); }, 1200);
                        /* */
                      },
                      'fail': function (res) {
                        wx.showToast({
                          title: '支付失败！',
                          image: '/pages/icon/delete.png',
                          duration: 1200,
                          mask: true,
                          complete: function () {
                         
                          } 
                        })
                  
                      },
                      'complete': function (res) {
                        console.log(res); console.log('complete');
                      }
                    })

                 

                  }
                }
            })


    // wx.request({
    //   url: '',
    //   data: '',
    //   header: { id: id, },
    //   success: function (res) {
    //     // 调用微信支付
    //     wx.requestPayment({
    //       'timeStamp': '',
    //       'nonceStr': '',
    //       'package': '',
    //       'signType': 'MD5',
    //       'paySign': '',
    //       'success': function (res) {

 

    //       },
    //       'fail': function (res) {
    //       }
    //     })
    //   },
    //   fail: function (res) { },
    //   complete: function (res) { },
    // })
  },
})

function prompt(title, status) {
  wx.showToast({
    title: title,
    icon: status,  //图标，支持"success"、"loading"
    duration: 1200,
    mask: true,
  })
}